package org.seed.rpc.core;


import lombok.extern.slf4j.Slf4j;
import org.seed.rpc.core.util.Ipv4IntValConvertor;

import java.util.*;


/**
 * 注册中心操作
 */
@Slf4j
public class RegistryOperator {


    /**
     * 获取全部的service和endpoint的映射
     *
     * @param client
     * @return
     */
    public static Map<String, List<String>> getServiceEndpointMap(RegistryClient client) {
        Map<String, String> map = client.getPrefix(Constants.SERVICE_REGISTRY_KEY_PREFIX);
        Map<String, List<String>> rst = new HashMap<>();
        if (map == null || map.size() == 0) {
            return rst;
        }
        for (Map.Entry<String, String> entry : map.entrySet()) {
            String key = entry.getKey();
            String endpoint = entry.getValue();
            key = key.replace(Constants.SERVICE_REGISTRY_KEY_PREFIX, "");
            if (key.indexOf("☼") < 0 || key.indexOf("✡") < 0) {
                continue;
            }
            String serviceName = key.substring(0, key.indexOf("☼"));
            List<String> endpointList = rst.get(serviceName);
            if (endpointList == null) {
                endpointList = new ArrayList<>();
            }
            if (!endpointList.contains(endpoint)) {
                endpointList.add(endpoint);
            }
            rst.put(serviceName, endpointList);
        }
        return rst;
    }


    /**
     * 将本地服务接口发布至注册中心
     *
     * @param serviceName 应用名称 (spring.application.name)
     * @param endpoint    当前应用的 IP:PORT
     */
    public static void publishLocalEndpoint(String serviceName, String endpoint, RegistryClient client) {
        String[] ipAndPort = endpoint.split(":");
        String ipStr = ipAndPort[0];
        String portStr = ipAndPort[1];
        int ipIntVal = Ipv4IntValConvertor.ipv4ToInt(ipStr);
        String key = Constants.SERVICE_REGISTRY_KEY_PREFIX + serviceName + "☼" + String.valueOf(ipIntVal) + "✡" + portStr;
        client.put(key, endpoint);
        EndpointChecker.start(key, endpoint, client);
    }


    /**
     * 注销本地服务节点
     *
     * @param serviceName 应用名称 (spring.application.name)
     * @param endpoint    当前应用的 IP:PORT
     */
    public static void unloadLocalEndpoint(String serviceName, String endpoint, RegistryClient client) {
        String[] ipAndPort = endpoint.split(":");
        String ipStr = ipAndPort[0];
        String portStr = ipAndPort[1];
        int ipIntVal = Ipv4IntValConvertor.ipv4ToInt(ipStr);
        String key = Constants.SERVICE_REGISTRY_KEY_PREFIX + serviceName + "☼" + String.valueOf(ipIntVal) + "✡" + portStr;
        client.remove(key);
    }


    /**
     * 测试注册中心网路连接
     *
     * @param client
     * @return
     */
    public static boolean testRegistryConnection(RegistryClient client) {
        try {
            client.put(Constants.NETWORK_CONNECTION_TEST_KEY, "OK");
            client.remove(Constants.NETWORK_CONNECTION_TEST_KEY);
            return true;
        } catch (Exception e) {
            return false;
        }
    }




}
